package com.cskaoyan.wx.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.controller.AuthController;
import com.cskaoyan.mapper.KeywordMapper;
import com.cskaoyan.mapper.SearchHistoryMapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.wx.bean.vo.WxSearchIndexVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @ description:
 * @ author:台
 **/

@Service
public class WxSearchServiceImpl implements WxSearchService {

    @Autowired
    KeywordMapper keywordMapper;

    @Autowired
    SearchHistoryMapper searchHistoryMapper;

    @Autowired
    UserMapper userMapper;

    static String username;
    static Integer id;

    @Override
    public WxSearchIndexVo searchIndex() {
        WxSearchIndexVo wxSearchIndexVo = new WxSearchIndexVo();
        //获取Vo的第一个成员
        KeywordExample keywordExample = new KeywordExample();
        KeywordExample.Criteria criteria = keywordExample.createCriteria();
        criteria.andIsDefaultEqualTo(true);
        criteria.andDeletedEqualTo(false);
        List<Keyword> keywords = keywordMapper.selectByExample(keywordExample);
        Keyword keyword = keywords.get(new Random().nextInt(keywords.size()));
        //获取Vo的第二个成员
        if (username == null){
            getIdAndUsername();
        }
        if (username == null || id == null){
            return new WxSearchIndexVo();
        }
        UserExample userExample = new UserExample();
        userExample.createCriteria().andUsernameEqualTo(username);
        List<User> users = userMapper.selectByExample(userExample);
        id = users.get(0).getId();
        List<SearchHistory> searchHistories = searchHistoryMapper.selectKeyword(id);
        //获得Vo的第三个成员
        KeywordExample keywordExample1 = new KeywordExample();
        KeywordExample.Criteria criteria1 = keywordExample1.createCriteria();
        criteria1.andIsHotEqualTo(true);
        criteria1.andDeletedEqualTo(false);
        List<Keyword> hotKeywords = keywordMapper.selectByExample(keywordExample1);

        wxSearchIndexVo.setDefaultKeyword(keyword);
        wxSearchIndexVo.setHistoryKeywordList(searchHistories);
        wxSearchIndexVo.setHotKeywordList(hotKeywords);
        return wxSearchIndexVo;
    }

    @Override
    public List<String> searchHelper(String keyword) {
        KeywordExample keywordExample = new KeywordExample();
        keywordExample.createCriteria().andKeywordLike("%" + keyword + "%");
        List<Keyword> keywords = keywordMapper.selectByExample(keywordExample);
        List<String> strings = new ArrayList<>();
        for (Keyword keyword1 : keywords) {
            strings.add(keyword1.getKeyword());
        }
//        if (keyword != null) {
////            SearchHistory searchHistory = new SearchHistory();
////            searchHistory.setUserId(id);
////            searchHistory.setKeyword(keyword);
////            Date date = new Date();
////            searchHistory.setAddTime(date);
////            searchHistory.setUpdateTime(date);
////            searchHistory.setFrom(username);
////            searchHistoryMapper.insertSelective(searchHistory);
////        }
        return strings;
    }

    @Override
    public void clearHistory() {

        SearchHistory searchHistory = new SearchHistory();
        searchHistory.setDeleted(true);
        searchHistory.setUpdateTime(new Date());
        //继续假装我们有个用户   id为1
        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
        SearchHistoryExample.Criteria criteria = searchHistoryExample.createCriteria();
        criteria.andUserIdEqualTo(id);
        searchHistoryMapper.updateByExampleSelective(searchHistory,searchHistoryExample);
    }

    private void getIdAndUsername() {
        Subject subject = SecurityUtils.getSubject();
        username = (String) subject.getPrincipal();
        if (username == null)return;
        UserExample userExample = new UserExample();
        userExample.createCriteria().andUsernameEqualTo(username);
        List<User> users = userMapper.selectByExample(userExample);
        id = users.get(0).getId();
    }
}
